from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QVBoxLayout, QPushButton, QLabel

class ExampleApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        layout = QVBoxLayout(self)

        # 创建一个按钮,点击后打开文件对话框
        self.openButton = QPushButton("打开文件", self)
        self.openButton.clicked.connect(self.openFileDialog)
        layout.addWidget(self.openButton)

        # 显示选中的文件路径
        self.label = QLabel("未选择文件", self)
        layout.addWidget(self.label)

    def openFileDialog(self):
        # 打开文件对话框
        options = QFileDialog.Options()
        fileName, _ = QFileDialog.getOpenFileName(self, "选择文件", "", "所有文件 (*);;文本文件 (*.txt)", options=options)
        if fileName:
            self.label.setText(f"选中的文件:{fileName}")

if __name__ == "__main__":
    app = QApplication([])
    ex = ExampleApp()
    ex.show()
    app.exec_()